package cn.hinglo.gameclaw.user.action;

import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.FileImageUtils;
import cn.hinglo.gameclaw.user.service.ImageService;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @createTime: 2018/1/2
 * @author: HingLo
 * @description: 图片响应类
 */
@RestController
@RequestMapping("user/i/")
public class ImageAction extends BaseController {
    private final ImageService imageService;

    @Autowired
    public ImageAction(ImageService imageService) {
        this.imageService = imageService;
    }

    /**
     * 用于专业的查看图片
     *
     * @param id   图片类型的对应Id
     * @param name 图片的名称
     * @param type 响应的类型,根据类型然后去读取不同位置的下的图片
     */
    @GetMapping("image")
    public Result imageResponse(String name, String id, Integer type) {
        //通过Id去数据库查询该Id对应的图片名称
        if (StrUtil.isBlank(name) && StrUtil.isNotBlank(id)) {
            name = imageService.findImageNameService(id, type);
        }
        if (StrUtil.isNotBlank(name)) {
            byte[] inputStream = FileImageUtils.readImageUtils(name, type);
            if (inputStream != null) {
                return success("data:image/png;base64," + Base64.encode(inputStream));
            }
        }
        return error("图片没有哦");
    }

}